import { createRouter, createWebHistory } from "vue-router"
import Home from "@/views/Layout/index.vue"
import Login from "@/views/Login/index.vue"
import { ElMessage } from "element-plus";
const routes = [
  {
    path: "/admin",
    component: () => import("@/admin/index.vue"),
    name: "admin",
    children: [
      {
        path: "",
        component: () => import("@/admin/views/Home/index.vue"),
        name: "adminHome",
        children: [
          {
            path: "",
            name: "adminHome",
            component: () => import("@/admin/components/Welcome.vue"),
          },
          {
            path: "product",
            name: "product",
            component: () => import("@/admin/views/Product/index.vue"),
          },
          {
            path: "product/add",
            name: "add",
            component: () => import("@/admin/views/Product/components/Add.vue"),
          },
          {
            path:"user",
            name:"userList",
            component:()=>import("@/admin/views/UserList/index.vue")
          },
          {
            path:"recharge",
            name:"recharge",
            component:()=>import("@/admin/views/Recharge/index.vue")
          },
        ],
      },
      {
        path: "login",
        name: "adminLogin",
        component: () => import("@/admin/views/Login/index.vue"),
      },
    ],
  },
  {
    path: "/",
    component: Home,
    children: [
      {
        path: "/",
        component: () => import("@/views/Layout/views/Home.vue"),
      },
      {
        path: "/product/:id",
        name: "productDetail",
        component: () => import("@/views/Product/index.vue"),
        params: { id: Number },
      },
      {
        path: "/product/list",
        name: "productList",
        component: () => import("@/views/ProductList/index.vue"),
      },
      {
        path: "/address",
        name: "address",
        component: () => import("@/views/Address/index.vue"),
      },
      { path: "/login", component: Login },
      { path: "/register", component: () => import("@/views/Register/index.vue") },
      {
        path: "/password_reset",
        component: () => import("@/views/ForgetPassword/index.vue"),
      },
      {
        path: "/cart",
        component: () => import("@/views/ShoppingCart/index.vue"),
      },
      {
        path: "/checkout/:id",
        name: "checkout",
        component: () => import("@/views/CheckOut/index.vue"),
        params: { id: String },
      },
      {
        path: "/pay/:id",
        name: "payOrder",
        component: () => import("@/views/Pay/index.vue"),
        params: { id: String },
      },
      {
        path: "/order",
        component: () => import("@/views/Order/index.vue"),
      },

    ],
  },
  {
    path: "/:pathMatch(.*)*",
    component: () => import("@/views/404/index.vue"),
  }
];
const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 路由守卫
function isPublicRoute(path) {
  return (path === "/" 
    || path === "/login" 
    || path === "/register" 
    || path === "/password_reset"
    || path.startsWith("/product")
  );
}

function isAdminPublicRoute(path) {
  return path === "/admin/login";
}

function isAuthenticated(to, next) {
  const token = localStorage.getItem("user_token");
  if (token) {
    next();
  } else {
    ElMessage.error("请先登录");
    next("/login");
  }
}

function isAdminAuthenticated(to, next) {
  const adminToken = localStorage.getItem("admin_token");
  if (adminToken) {
    next();
  } else {
    ElMessage.error("请先登录管理员账号");
    next("/admin/login");
  }
}

router.beforeEach((to, from, next) => {
  if (to.path.startsWith('/admin')) {
    if (isAdminPublicRoute(to.path)) {
      next();
    } else {
      isAdminAuthenticated(to, next);
    }
  } else {
    if (isPublicRoute(to.path)) {
      next();
    } else {
      isAuthenticated(to, next);
    }
  }
});

export default router;

